<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Mail extends CI_Controller {
  public function __construct() {
    parent::__construct();
    auth_user();
  }

	/**
	 * This is the home page with the mail section details
	 */
	public function index() {
    $data['adminview'] = 'admin/tpls/mail_dash_view';
    $data['data'] = array();

    $this->load->view('admin/layouts/adminpage',$data);
  }

	/**
	 * This page returns the html for Angular to show the latest newsletter
	 */
	public function viewnewsletter() {
		$query = $this->db->select()->from('ci_newsletters')->where('status',0)->get()->row();
		echo $query->data;
	}

	/**
	 * This is the page where the latest article is taken and
	 * the html for the newsletter is generated
	 * and saved in to the database table.
	 */
	public function newsletter_html() {
//		loading the libraries and models
		$this->load->library('parser');
    $this->load->model('alias_model', 'alias');
		$this->load->model('admin/mail_model');

		$this->mail_model->final_newsletter_html();



		redirect('admin/admindash/data#/newsletter/dashboard');
	}

	/**
	 * This page is returning the view data to angular.
	 */
	public function maildashboard() {
		$this->load->view('admin/tpls/mail_dash_view');
	}

	/**
	 * This is where the newsletter html is being send as a mail.
	 */
	public function send_new_post_mail() {
//		loading the assets
		$this->load->model('admin/mail_model');

//		getting the latest newsletter
		$query_newsletter = $this->db->select()->from('ci_newsletters')->where('status',0)->where('type', 'new post')->get()->row();
		$newsletter_id = $query_newsletter->nid;
		$newsletter_html = $query_newsletter->data;
//		dsm($newsletter_html,1);

//		getting the subscribers
		$subscribers = $this->mail_model->get_subscribers($newsletter_id);
	}

}